<?php

namespace app\common\util;

use app\admin\service\AdminPhoto;

class Upload
{

    public static function putFile($file, $type, $path)
    {
        $path = $path ?: 'default';
        global $globalConfig;
        $fileType = $globalConfig['file_type'] ?? 1;
        try {
            if ($type == 'image') {
                $fileSize = !empty($globalConfig['imgValidate_size']) ? $globalConfig['imgValidate_size'] * 1024 : config('procedure.uploadValidate.imgValidate.validate.size');
                $fileExt = implode(',', explode('|', !empty($globalConfig['imgValidate_ext']) ? $globalConfig['imgValidate_ext'] : config('procedure.uploadValidate.imgValidate.validate.ext')));
            } elseif ($type == 'video') {
                $fileSize = !empty($globalConfig['videoValidate_size']) ? $globalConfig['videoValidate_size'] * 1024 : config('procedure.uploadValidate.videoValidate.validate.size');
                $fileExt = implode(',', explode('|', !empty($globalConfig['videoValidate_ext']) ? $globalConfig['videoValidate_ext'] : config('procedure.uploadValidate.videoValidate.validate.ext')));
            } elseif ($type == 'audio') {
                $fileSize = !empty($globalConfig['audioValidate_size']) ? $globalConfig['audioValidate_size'] * 1024 : config('procedure.uploadValidate.audioValidate.validate.size');
                $fileExt = implode(',', explode('|', !empty($globalConfig['audioValidate_ext']) ? $globalConfig['audioValidate_ext'] : config('procedure.uploadValidate.audioValidate.validate.ext')));
            } else {
                $fileSize = !empty($globalConfig['fileValidate_size']) ? $globalConfig['fileValidate_size'] * 1024 : config('procedure.uploadValidate.fileValidate.validate.size');
                $fileExt = implode(',', explode('|', !empty($globalConfig['fileValidate_ext']) ? $globalConfig['fileValidate_ext'] : config('procedure.uploadValidate.fileValidate.validate.ext')));
            }
            validate(['file' => [
                'fileSize' => $fileSize,
                'fileExt' => $fileExt,
            ]])->check(['file' => $file]);
        } catch (\think\exception\ValidateException $e) {
            return ['msg' => '上传失败', 'code' => 201, 'data' => $e->getMessage()];
        }
        foreach ($file as $k) {
            if ($fileType == 2) {
                $name = $k->getOriginalName();
                //阿里云上传
                if ($k['size'] <= 1024 * 1024) $res = Oss::alYunOSS($k, $k->extension(), $path);
                else $res = Oss::fileToOssFragmentation($path);
                if ($res["code"] == 201)
                    return ['msg' => '上传失败', 'code' => 201, 'data' => [$res["msg"]]];
                $url = $res['src'];
                AdminPhoto::add($k, $url, $path, 2);
            } elseif ($fileType == 3) {
                $name = $k->getOriginalName();
                //七牛上传
                $res = Qiniu::QiniuOSS($k, $k->extension(), $path);
                if ($res["code"] == 201) {
                    return ['msg' => '上传失败', 'code' => 201, 'data' => [$res["msg"]]];
                }
                $url = $res['src'];
                AdminPhoto::add($k, $url, $path, 3);
            } else {
                $name = $k->getOriginalName();
                $saveName = '/' . 'upload' . '/' . \think\facade\Filesystem::disk('public')->putFile($path, $k);
                $url = str_replace("\\", "/", $saveName);
                AdminPhoto::add($k, $url, $path, 1);
            }
        }
        return ['msg' => '上传成功', 'code' => 0, 'data' => ['src' => dealImage($url), 'thumb' => $name]];
    }
}
